package middleware

import (
	"gitee.com/crack007/goose/core/web"
	"gitee.com/crack007/udp-penetrate/src/common/exception"
	"github.com/gin-gonic/gin"
)

func JsonResponseMiddleware(ctx *gin.Context) {
	if ctx.Keys == nil {
		ctx.Keys = map[string]interface{}{}
	}
	ctx.Next()
	dto := web.GetResponse(ctx)
	if v, ok := dto.(*exception.ApiException); ok {
		responseError(ctx, v.Code, v.Msg)
		return
	}
	ctx.JSON(200, gin.H{
		"msg":  "",
		"code": 200,
		"data": dto,
	})
}
func responseError(context *gin.Context, code int32, err string) {
	context.JSON(200, gin.H{
		"msg":  err,
		"code": code,
		"data": nil,
	})
}
